# -*- coding:utf-8 -*-
from selenium.webdriver.common.by import By

from PageClass.loginPageClass.publicLoginPage import PublicLoginPage
from Util import logger, ZTCCLOUDError


class LoginPage(PublicLoginPage):
	_loginKey = (By.ID, "loginKey")
	_password = (By.ID, "password")
	_loginButton = (By.ID, "login")
	_loginSuccess = (By.XPATH, "//header")
	_IntoButton = (By.XPATH, '//*[@id="app"]/div/div[2]/div/div[2]/div/div[2]/p[2]')

	# 有可能出现错误提示的元素位置
	_MessageBox = (By.CLASS_NAME, 'el-message__content')
	_accountInputErrorBox = (By.XPATH, '//*[@id="app"]/div/div[1]/div/form/div[1]/div/div')
	_passwordInputErrorBox = (By.XPATH, '//*[@id="app"]/div/div[1]/div/form/div[2]/div/div')

	# 登录成功标识
	_loginSuccessLogo = (By.XPATH, '//div[@class="login-info"]')

	def __init__(self, driver):
		super().__init__(driver)
		self.driver.maximize_window()
		self.driver.implicitly_wait(10)

	@property
	def intoButton(self):
		return self._IntoButton

	@property
	def messageBox(self):
		return self._MessageBox

	@property
	def accountInputErrorBox(self):
		return self._accountInputErrorBox

	@property
	def passwordInputErrorBox(self):
		return self._passwordInputErrorBox

	@property
	def loginSuccessBox(self):
		return self._loginSuccess

	def goToLoginPage(self, url):
		self.driver.get(url)
		logger.info("打开登录页面")

	def inputAccount(self, account):
		self.clear(*self._loginKey)
		self.send_text(account, *self._loginKey)
		logger.info("输入帐号")

	def inputPassword(self, password):
		self.clear(*self._password)
		self.send_text(password, *self._password)
		logger.info("输入密码")

	def clickLoginButton(self):
		self.click(*self._loginButton)
		logger.info("点击登录")

	def get_into(self):
		self.click(*self._IntoButton)

	def loginSuccessOrFail(self):
		try:
			loginMsg = self.find_element(*self._loginSuccessLogo).text
		except ZTCCLOUDError:
			return False
		else:
			if "欢迎您" in loginMsg:
				return True
			else:
				return False

	def getErrorMessage(self):
		return self.get_elementText(*self.messageBox)

	def getUserAccountErrorMessage(self):
		return self.get_elementText(*self.accountInputErrorBox)

	def getPasswordErrorMessage(self):
		return self.get_elementText(*self.passwordInputErrorBox)

	def getLoginSuccessMessage(self):
		return self.get_elementText(*self.loginSuccessBox)


